package cn.com.fssg.platform.base.handler.impl.cl.fuju;

import cn.com.fssg.platform.base.handler.FormHandler;
import cn.com.fssg.platform.biz.BizUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.text.SimpleDateFormat;
import java.util.*;

@Component
public class ClFujuSubmitFormHandler implements FormHandler {
//    private NamedParameterJdbcTemplate jdbcTemplate = BizUtil.cur().tenantMainJdbcTemplate();
    @Autowired
    private NamedParameterJdbcTemplate jdbcTemplate;

    private SimpleDateFormat timeFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    @Override
    public boolean beforeSave(String formCode, String keyName, String keyValue, FormData formData) {
        return true;
    }

    @Override
    public boolean afterSave(String formCode, String keyName, String keyValue, FormData formData) {
        //提交后保存辅具信息
        Map<String, Object> main = formData.getMain();
//        Map<String, Object> sub = formData.getSub();
        String applyId = (String) main.get("id");

        StringBuilder builder = new StringBuilder();
        builder.append("insert into cl_fuju_item (");
        builder.append("id, id_card, money, apply_status,");
        builder.append("shenpi_no, shenpi_no_sub, fuju_cat,");
        builder.append("fuju_type, fuju_info, apply_id, count,");
        builder.append("beizhu, create_by, create_time, update_by, update_time,");
        builder.append("del_flag, fuju_id)");
        builder.append("values(");
        builder.append("'%s', '%s', %s, '%s',");
        builder.append("'%s', '%s', '%s',");
        builder.append("'%s', '%s', '%s', %s,");
        builder.append("'%s', '%s', '%s', '%s', '%s',");
        builder.append("'%s','%s')");
        String sql = builder.toString();
        String cjrIdcard = (String) main.get("cjrIdcard");
        String shenpiNo = (String) main.get("shenpiNo");
        String insertSql, shenpiNoSub;
        String userId = BizUtil.cur().curUserId();
        String currentTime = timeFormat.format(new Date());
        //辅具可能存在多个
        List<Map<String, Object>> items = (List<Map<String, Object>>) main.get("items");
        if (!CollectionUtils.isEmpty(items)) {
            Map<String,Object> paraMap = new HashMap<String,Object>();
            int index = 1;
            for (Map<String,Object> item : items) {
                shenpiNoSub = shenpiNo + "_" + index;
                //写入cl_apply_fuju_item
                insertSql = String.format(sql,
                        BizUtil.cur().getSnowflakeIdStr(), cjrIdcard, item.get("money"), "0",
                        shenpiNo, shenpiNoSub, item.get("fuju_cat"),
                        item.get("fuju_type"), item.get("fuju_info"), applyId, item.get("count"),
                        item.get("beizhu"), userId, currentTime, userId, currentTime,
                        "0", item.get("fuju_id")
                        );
                jdbcTemplate.update(insertSql, paraMap);
                index++;
            }
        }
        return true;
    }

    @Override
    public boolean afterInit(String formCode, String keyName, String keyValue, FormData formData) {
        return false;
    }

    @Override
    public Map<String, Object> afterInit(String formCode, String keyName, String keyValue, FormData formData, Map<String, Object> dataRow) {
        return Collections.emptyMap();
    }

    @Override
    public boolean afterDelete(String formCode, String keyName, String keyValue, FormData formData) {
        return false;
    }
}
